package controllers

import (
	"github.com/XieWeikai/PlaceToGo/models"
	"github.com/gin-gonic/gin"
	_ "github.com/go-playground/validator/v10"
	"net/http"
)

// 定义请求结构体并包含验证规则
type BrokerSummaryRequest struct {
	Start  string `form:"start" binding:"required,datetime=200601"`
	End    string `form:"end" binding:"required,datetime=200601"`
	Region string `form:"region" binding:"required"`
}

// 定义请求结构体并包含验证规则
type BrokerSummaryByTimeRequest struct {
	Start     string `form:"start" binding:"required,datetime=200601"`
	End       string `form:"end" binding:"required,datetime=200601"`
	Region    string `form:"region" binding:"required"`
	PlaceType string `form:"placeType" binding:"required"`
}

func GetBrokerSummary(c *gin.Context) {
	// /broker_summary?start=202101&end=202109&region=广东-深圳
	if c.MustGet("user_type").(string) != "admin" {
		c.JSON(403, gin.H{"error": "forbidden"})
		return
	}

	// 解析请求参数
	var req BrokerSummaryRequest
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// 调用业务逻辑
	results, err := models.GetSummary(req.Start, req.End, req.Region)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
		return
	}

	// 返回结果
	c.JSON(http.StatusOK, &results)
}

func GetBrokerSummaryByTime(c *gin.Context) {
	// /broker_summary_by_time?start=202101&end=202109&region=广东-深圳&place_type=酒店
	if c.MustGet("user_type").(string) != "admin" {
		c.JSON(403, gin.H{"error": "forbidden"})
		return
	}

	// 解析请求参数
	var req BrokerSummaryByTimeRequest
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// 调用业务逻辑
	results, err := models.GetSummaryByTime(req.Start, req.End, req.Region, req.PlaceType)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
		return
	}

	// 返回结果
	c.JSON(http.StatusOK, &results)
}
